{% extends 'users/user-base.html' %}

{% load static %}
{% block title %}现有域{% endblock %}
{% block custom_css %}
    {#    <link href="css/domain.css" rel="stylesheet" title="" type="text/css"/>#}
    <style>
        td{
            vertical-align:middle !important;
        }
    </style>
{% endblock %}

{% block custom_js %}{% endblock %}

{% block contentdomain %}
    <script type="text/javascript">
        $(document).ready(function () {
            $("#tab").on("click", ":button", function (event) {
                console.log($(this).attr('id'));
                var btn_id = $(this).attr('id');
                var domain_name = $(this).closest("tr").find("td").eq(1).text();
                var domain_ip = $(this).closest("tr").find("td").eq(3).text();
                console.log(domain_name);
                if (btn_id == 'sync') {
                    $.ajax({
                        type: "POST",
                        data: {
                            'domain_name': domain_name,
                            'domain_ip': domain_ip,
                        },
                        url: "/admins/sync_domain/",
                        success: function (data) {
                            if (data["st"] === 1) {
                                alert("成功");
                                window.location.reload();
                            }
                        },
                        error: function () {
                            alert("失败");
                        }
                    })
                } else if (btn_id == 'del') {
                    $.ajax({
                        type: "POST",
                        data: {
                            'domain_name': domain_name,
                            'domain_ip': domain_ip,
                        },
                        url: "/admins/delete_domain/",
                        success: function (data) {
                            if (data["st"] === 1) {
                                alert("成功");
                                window.location.reload();
                            }else if (data["st"] === 2){
                                alert("删除失败，请先删除域内所有服务节点");
                            }

                        },
                        error: function () {
                            alert("失败");
                        }
                    })
                } else if (btn_id == 'views') {
                    {#$.ajax({#}
                    {#    type: "POST",#}
                    {#    data: {#}
                    {#        'domain_name': domain_name,#}
                    {#        'domain_ip': domain_ip,#}
                    {#    },#}
                    {#    url: "/admins/existing_workstation/",#}
                    {#    success: function (data,textStatus) {#}
                    {#            if (data) {#}
                                    {#window.location.href = '/admins/existing_workstation/';#}
                    {#            }#}
                    {#            else{#}
                    {#                alert("失败")#}
                    {#            }#}
                    {#        }#}
                    {#     }#}
                    {#)#}
                    window.location.href = '/admins/existing_workstation/?domain_name='+domain_name+'&domain_ip='+domain_ip
                }
            })
        })

    </script>
        <div class="dashboard-wrapper">
            <div class="dashboard-ecommerce">
                <div class="container-fluid dashboard-content ">
                    <!-- ============================================================== -->
                    <!-- pageheader  -->
                    <!-- ============================================================== -->
                    <div class="row">
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="page-header">
                                <h2 class="pageheader-title">现有域</h2>
                                <div class="page-breadcrumb">
                                    <nav aria-label="breadcrumb">
                                        <ol class="breadcrumb">
                                            <li class="breadcrumb-item">域管理</li>
                                            <li class="breadcrumb-item active" aria-current="page">现有域</li>
                                        </ol>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- ============================================================== -->
                    <!-- end pageheader  -->
                    <!-- ============================================================== -->

                    <div class="row">
                        <!-- ============================================================== -->
                        <!-- basic table  -->
                        <!-- ============================================================== -->
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="card">
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered first" id="tab">
                                            <thead>
                                            <tr>
                                                <th class="table-id">ID</th>
                                                <th class="domain-name">域名</th>
                                                <th class="domain-location">所在地</th>
                                                <th class="gw-ip">网关地址</th>
                                                <th class="gw-port">端口</th>
                                                <th class="gw-user">用户名</th>
                                                <th class="gw-pwd">密码</th>
                                                <th class="deploy-time">部署时间</th>
                                                <th class="sync-time">上次同步时间</th>
                                                <th class="vt-type">虚拟化</th>
                                            </tr>
                                            </thead>
                                            <form id="domainform">
                                                {% for domain in data %}
                                                    <tr>
                                                        <td>{{ forloop.counter }}</td>
                                                        <td>{{ domain.domain }}</td>
                                                        <td>{{ domain.location }}</td>
                                                        <td>{{ domain.gw_ip }}</td>
                                                        <td>{{ domain.port }}</td>
                                                        <td>{{ domain.user }}</td>
                                                        <td>{{ domain.pwd }}</td>
                                                        <td>{{ domain.deploy_time }}</td>
                                                        <td>{{ domain.sync_time }}</td>
                                                        <td>{{ domain.vt_type }}</td>
                                                        {% if domain.state == 1 %}
                                                            <input type="hidden" id="sync_domain" name="is_sync"
                                                                   value={{ domain.id }}>
                                                            <td>
                                                                <button type="button" class="btn btn-success" id="sync" name="sync">
                                                                    <span class="glyphicon glyphicon-refresh"></span>
                                                                    同步
                                                                </button>
                                                                <button type="button" class="btn btn-danger" id="del" name="del">
                                                                    <span class="glyphicon glyphicon-trash"></span>
                                                                    删除
                                                                </button>
                                                                <button type="button" class="btn btn-primary" id="views" name="views">
                                                                    <span class="glyphicon glyphicon-eye-open"></span>
                                                                    查看节点
                                                                </button>
                                                            </td>
                                                        {% endif %}
                                                    </tr>
                                                {% endfor %}
                                            </form>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


{% endblock %}